﻿Ext.define("PSI.MainMenu.Admin.MainForm", {

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: false,
            bodyBorder: false,
            layout: "fit",
            items: [
               this.getMainPanels(),
              //  this.basecodeAttrPanle()
            ]
        });

        me.panel = panel;
    },

    getMainPanel: function () {
        return this.panel;
    },

    getMainPanels: function () {
       
        var mainPanel = Ext.create("Ext.panel.Panel", {
            title: "菜单管理",
            tbar: [
             { xtype: 'button', text: '新增菜单', iconCls: "psi-button-add", handler: this.showAddMenuWin, scope: this },
             { xtype: 'button', text: '编辑菜单', iconCls: "psi-button-edit", handler: this.editMenu, scope: this },
             { xtype: 'button', text: '删除菜单', iconCls: "psi-button-delete", handler: this.deleteMenu, scope: this }
            ],
            items: [
                this.getMenuTree()
            ]
        })
        return mainPanel;
    },

    getMenuTree: function () {

        var me = this;

        Ext.define("PSIMenuModel", {
            extend: "Ext.data.Model",
            fields: ["id", "caption", "expandable", "expanded", "leaf", "children", "fid"]
        });

        var MenuStore = Ext.create("Ext.data.TreeStore", {
            id: "treestore",
            model: "PSIMenuModel",
            proxy: {
                type: "ajax",
                actionMethods: { read: 'POST' },
                url: "/MainMenuAdmin/GetTreeItem"
            }
        });

        var MenuTree = Ext.create('Ext.tree.Panel', {      
            id: "menutree",
            border: false,
            rootVisible: false,                 // 这个比较重要  隐藏根节点
            useArrows: true,                    //树结构显示风格
            hideHeaders: true,                  //隐藏标题
            renderTo: Ext.getBody(),
            store: "treestore",
            columns: {
                items: [{
                    xtype: 'treecolumn',
                    dataIndex: 'caption',
                    text: "树根",
                    flex: 1
                }]
            }

        });

        this.menuTree = MenuTree;

        return MenuTree;

    },

    showAddMenuWin: function () {

        var form = Ext.create("PSI.BaseCode.Admin.MenuEdit");

        form.show();
    },

    deleteMenu: function () {

        var treegridselect = Ext.getCmp("menutree").getSelectionModel().getSelection();

        var treegrid = Ext.getCmp("menutree").getStore();

        if (treegridselect == null || treegridselect.length == 0)
        {
            PSI.MsgBox.showInfo("请选择一条记录！");
            return false;
        }

        var org = treegridselect[0].data;

        PSI.MsgBox.confirm("请确认是否删除记录",
              function () {
                  Ext.getBody().mask("正在删除中...");
                  Ext.Ajax.request({
                      url: "/MainMenuAdmin/DelMenuItem",
                      method: "POST",
                      params: { id: org.id },
                      callback: function (options, success, response) {
                          Ext.getBody().unmask();

                          if (success) {
                              var data = Ext.JSON.decode(response.responseText);
                              if (data.success) {
                                  PSI.MsgBox.showInfo("成功完成删除操作", function () {
                                      treegrid.load();
                                  });
                              } else {
                                  PSI.MsgBox.showInfo(data.msg);
                              }
                          } else {
                              PSI.MsgBox.showInfo("网络错误", function () {
                                  window.location.reload();
                              });
                          }
                      }

            });
       });

    },

    editMenu: function () {

        var treegridselect = Ext.getCmp("menutree").getSelectionModel().getSelection();

        var treegridstore = Ext.getCmp("menutree").getStore();

        if (treegridselect == null || treegridselect.length == 0) {
            PSI.MsgBox.showInfo("请选择一条记录！");
            return false;
        }

        var item = treegridselect[0].data

        if (item.parentId != null && item.parentId != "root")
        {
            var node= treegridstore.getNodeById(item.parentId);

            item.qtitle = node.raw.caption;
        }
        
        var form = Ext.create("PSI.BaseCode.Admin.MenuEdit");

        form.show(item);

    }

})